package com.bw.Threads.pattern_Lock.pattern;

import java.util.List;

public class Consumer implements Runnable{
    private List<String> backet;
    private LockObject lockObj;

    public Consumer(List<String> backet, LockObject lockObj) {
        this.backet = backet;
        this.lockObj = lockObj;
    }

    @Override
    public void run() {
        while (true){
            lockObj.lock.lock();//加锁
            while (backet.size() == 0){
                try {
                    System.out.println("消费完毕。没有可消费的了");
                    lockObj.lockCondition.await();//等待线程
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            backet.remove(0);//消费一个
            System.out.println(Thread.currentThread().getName() + "，包子数量为：" + backet.size());
            lockObj.lockCondition.signalAll();//唤醒所有线程
        }
    }
}
